#include <stdio.h>   // printf
#include <stdlib.h>  // exit
#include <unistd.h>
#include "htr/capi.h"

/*--------------------------------------------------------------------
*/
struct sArgs {
  char *host;
  int port;
};

/*--------------------------------------------------------------------
*/
struct sArgs glbArgs = { "localhost", 7000 };

/*--------------------------------------------------------------------
*/
static void s_test_args(int argc, char *argv[])
{
  if (argc == 2) {
    printf("\n\nhometree c-client example\n");
    printf("usage  : htr_test2_c_client [host port]\n");
    printf("example: htr_test2_c_client \"localhost\" 7000\n\n");
    exit(1);
  }
 
  if (argc == 3) {
    glbArgs.host = argv[1];
    glbArgs.port = atoi(argv[2]); 
  } 
}

/*--------------------------------------------------------------------
*/
int cbValue(sHtrResource s)
{
  double d = htrMimeDataDoubleValue(s,NULL);
  printf("Value changed: %d\n",(int)d);
  return 0;
}

/*--------------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
  sHtrClient   c;
  sHtrResource r;
  int          loopCnt = 0;
 
  s_test_args(argc,argv);
  printf("HTR-Connection: %s:%d\n",glbArgs.host,glbArgs.port); 

  // Erstmals brauchen wir eine Verbindung zum Server: 
  htrInit(argc,argv);
  htrClientCreate(&c);
  htrClientConnect(c,glbArgs.host,glbArgs.port);

  // alles klar.. weiter mit der Mime-Resource:
  htrMimeDataCreate(&r,c,"htree/tests/slider");
  htrResValueCallback(r,cbValue);
  htrMimeDataOpen(r,HTR_RES_OPEN_READ | HTR_RES_OPEN_CREATE);

  while (1) {
    printf("sleeping...\n");
    htrSleep(100);
    if (loopCnt++ > 10) {
      printf("doing some very import IO-stuff...\n");
      usleep(1000000);
      loopCnt = 0;
    }
  }
  return 0;
}

